#include "ConfigManager.h"
#include "include/IFileSystem.h"
#include "include/ILog.h"
#include <cstdio>
#include <cstring>
#include <stdlib.h>

#define SECTION_SEPARATOR              "::->"
#define SUBSECTION_SEPARATOR        "||||"

CConfigManager::CConfigManager()
{
    actualsection = DEFAULT_SEC;
	pLogRelease << "ConfigManager created..." << endl;
}

CConfigManager::~CConfigManager()
{
}

void CConfigManager::SetActualSection(stringc Section)
{
    actualsection = Section;
}

stringc CConfigManager::GetActualSection()
{
    return actualsection;
}

bool CConfigManager::ReadFile(stringc File)
{
    IIOFile *file = PLE_FileSystem->GetFile(File);
    if (file)
    {
        stringc str="";
        stringc EnvName="";
        stringc VarName="";
        stringc VarValue="";

        if (!file->GetBuffer())
            file->LoadEntireFile();

        u8 *Buffer = file->GetBuffer();
        u32 PointerRead=0;

        char Var[200];
        char Var2[200];
        char Var3[200];

        while (PointerRead < file->fd.Size)
        {
            s32 elem = sscanf((const char*)(Buffer + PointerRead),"%s",Var);
            if (elem >=0)
            {
                str = Var;
                if (str[0] == '[')
                {
                    EnvName = str.substr(1,str.size()-2);
                }
                else
                {
                    sscanf((const char*)(Buffer + PointerRead),"%s = %s",Var2,Var3);
                    VarName = Var2;
                    VarValue = Var3;
                    str = VarName + " = " + VarValue;

                    stringc VarComplete = "";
                    VarComplete = actualsection + SECTION_SEPARATOR + EnvName + SUBSECTION_SEPARATOR + VarName;
                    configmap[VarComplete] = VarValue;

                    pLogInfo << "ConfigManager:: Readed variable into section: " << actualsection << ". Subsection: " << EnvName << ". Name: " << VarName << ". Value: " << VarValue << endl;
                }
            }
            else
            {
                str= "";
            }

            u32 Lenstr = str.size();
            PointerRead +=  Lenstr + 1;
        }

        return true;
    }
    else
    {
        pLogErrorDebug << "Configuration file " << File << " not found" << endl;
        return false;
    }
}

s32 CConfigManager::GetVarAsInt(stringc SubSection,stringc Name)
{
    stringc Var = "";
    Var = GetVarAsString(SubSection,Name);
    s32 temp;

    temp = atoi(Var.c_str());
    return temp;
}

f32 CConfigManager::GetVarAsFloat(stringc SubSection,stringc Name)
{
    stringc Var = "";
    Var = GetVarAsString(SubSection,Name);
    f32 temp;

    temp = (f32)atof(Var.c_str());
    return temp;
}

stringc CConfigManager::GetVarAsString(stringc SubSection,stringc Name)
{
    stringc Temp = "";
    stringc VarComplete = "";
    VarComplete = actualsection + SECTION_SEPARATOR + SubSection + SUBSECTION_SEPARATOR + Name;

    Temp = configmap[VarComplete].c_str();
    if (Temp.length() > 1)
    {
		if (Temp[0] == '\"' && Temp[Temp.length() - 1] == '\"')
		{
			stringc NameTemp = Temp.substr(1,Temp.length() - 2);
			Temp = GetVarAsString(SubSection,NameTemp);
		}
    }

    return Temp;
}
